/**
 * 
 */
package com.example.radioclub.music.tasks;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import android.os.AsyncTask;

import com.example.radioclub.data.Constants;
import com.example.radioclub.interfaces.http.IHttpJSONDownloader;
import com.facebook.Response;

/**
 * This class uses RestTemplate instance to work. This will try to reach
 * Internet connection and use Melon APIs to get response in String with JSON
 * format.
 * 
 * @author wizard
 * 
 */
public class RestJSONDownloader extends AsyncTask<String, Void, String> {
	private final String TAG = Constants.APP_TAG + "Rest JSON Downloader";
	private RestTemplate mapiRestTemplate;
	private IHttpJSONDownloader listener;
	private String apiAddress;
	
	public RestJSONDownloader(RestTemplate restTemplate, IHttpJSONDownloader listener, String apiAddress) {
		this.mapiRestTemplate = restTemplate;
		this.listener = listener;
		this.apiAddress = apiAddress;
	}
	
	public void setApiAddress(String apiAddress) {
		this.apiAddress = apiAddress;
	}
	
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		ResponseEntity<String> result = this.mapiRestTemplate.getForEntity(this.apiAddress, String.class);
		return result.getBody();
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		this.listener.onJSONDownloadFinished(result);
		super.onPostExecute(result);
	}

}
